package cn.tqfeiyang.smartadmin.business.organization.service;

import cn.tqfeiyang.smartadmin.business.organization.mapper.AddressMapper;
import cn.tqfeiyang.smartadmin.business.organization.entity.AddressEntity;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import jakarta.annotation.Resource;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 地址列表服务
 *
 * @author tqfeiyang
 * @since 2024-10-18
 */
@Service
public class AddressListService {

    /**
     * 地址映射接口
     */
    @Resource
    private AddressMapper addressMapper;

    /**
     * 查询地址列表
     *
     * @return 地址列表
     */
    public List<AddressEntity> getAddressList() {
        return addressMapper.selectList(Wrappers.emptyWrapper());
    }

    /**
     * 查询地址map
     *
     * @return 地址map
     */
    public Map<Long, AddressEntity> getAddressMap() {
        return getAddressList().stream()
                .collect(Collectors.toMap(AddressEntity::getAddressId, Function.identity()));
    }

    /**
     * 根据地址id查询地址信息
     *
     * @param addressId 地址id
     * @return 地址信息，如果不存在则返回null
     */
    public AddressEntity findAddressByAddressId(Long addressId) {
        Assert.notNull(addressId, "addressId不能为空");
        return addressMapper.selectById(addressId);
    }

}
